package com.qianchang.microfinance.view.base;


import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import com.dm.library.log.transaction.log.manager.LogManager;
import com.dm.library.log.transaction.utils.LogUtils;
import com.dm.library.utils.DMLog;
import com.dm.library.widgets.LoadingDialog;
import com.dm.library.widgets.statusbar.StatusBarUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.utils.AppManager;
import com.qianchang.microfinance.view.GuideActivity;
import com.qianchang.microfinance.view.MainActivity;
import com.qianchang.microfinance.view.SplashActivity;
import com.qianchang.microfinance.view.credit.data.facerecoder.FaceVideoRecordActivity;
import com.qianchang.microfinance.view.dialog.alert.DimengConfirmAlertDialog;
import com.qianchang.microfinance.view.dialog.alert.DimengOneEditAlertDialog;
import com.qianchang.microfinance.view.dialog.alert.DimengSingleButtonAlertDialog;
import com.qianchang.microfinance.view.dialog.alert.DimengTwoPwdEditAlertDialog;
import com.qianchang.microfinance.view.login.LoginActivity;
import com.qianchang.microfinance.view.mine.bill.MyBillActivity;
import com.qianchang.microfinance.view.mine.bill.MyBillDetailActivity;
import com.qianchang.microfinance.view.register.RegisterActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import hei.permission.PermissionActivity;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * 修 改 人:  jiaohongyun
 * 修改时间:  2016/6/20
 */
public abstract class DmBaseActivity extends PermissionActivity implements ShowProgressDialogListener {
    /**
     * 保存上一次点击时间
     */
    private SparseArray<Long> lastClickTimes;
    private static final long RECLICK_TIME = 800;

    /**
     * 加载进度 （如需修改样式，改show方法就可以了）
     */
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        lastClickTimes = new SparseArray<>();
        LogManager.getManager(getApplicationContext()).registerActivity(this);
        LogManager.getManager(getApplicationContext()).log(this.getLocalClassName(), "onCreate", LogUtils.LOG_TYPE_2_FILE_AND_LOGCAT);


        /***
         *  // 友盟推送 统计应用启动数据 此方法与统计分析sdk中统计日活的方法无关！
         *  如果不调用此方法，不仅会导致按照”几天不活跃”条件来推送失效，还将导致广播发送不成功以及设备描述红色等问题发生。
         *  可以只在应用的主Activity中调用此方法，但是由于SDK的日志发送策略，有可能由于主activity的日志没有发送成功，而导致未统计到日活数据。
         */
        PushAgent.getInstance(getApplicationContext()).onAppStart();            //友盟推送 第二步骤


        // 友盟SDK在统计Fragment时，需要关闭Activity自带的页面统计，
        // 然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
        MobclickAgent.openActivityDurationTrack(true);

    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        setStatusBar();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        setStatusBar();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        setStatusBar();
    }


    /**
     * 检查是否可执行点击操作 防重复点击
     *
     * @return 返回true则可执行
     */
    public boolean checkClick(int id) {
        Long lastTime = lastClickTimes.get(id);
        Long thisTime = System.currentTimeMillis();
        if (lastTime == null || thisTime - lastTime >= RECLICK_TIME) {
            DMLog.e("checkClick", thisTime);
            lastClickTimes.put(id, thisTime);
            return true;
        }
        return false;
    }

    public void show() {
        show(getString(R.string.loading));
    }

    @Override
    public void show(boolean canCancel) {
        show();
        loadingDialog.setCancelable(canCancel);
    }

    @Override
    public void show(String text, boolean canCancel) {
        show(text);
        loadingDialog.setCancelable(canCancel);
    }

    @Override
    public void show(String text) {


        if (!isShowing() && !isFinishing()) {
            if (loadingDialog == null)
                loadingDialog = LoadingDialog.getInstance(this);
            loadingDialog.setCancelable(false);
            loadingDialog.show(text);
        }
    }

    @Override
    public boolean isShowing() {
        if (loadingDialog == null) {
            return false;
        }
        return loadingDialog.isShowing();
    }

    @Override
    public void dismiss() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        dismiss();
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().removeActivity(this);
        lastClickTimes = null;
        LogManager.getManager(getApplicationContext()).unregisterActivity(this);
        super.onDestroy();
    }

    /**
     * 设置状态栏
     */
    protected void setStatusBar() {
        if (!(this instanceof SplashActivity) && !(this instanceof GuideActivity)
                && !(this instanceof MainActivity) && !(this instanceof MyBillActivity) && !(this instanceof LoginActivity)
                && !(this instanceof RegisterActivity) && !(this instanceof MyBillDetailActivity)&& !(this instanceof FaceVideoRecordActivity)) {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color_gold), 0);
        }
    }

    protected DimengSingleButtonAlertDialog showAlertDialog(String message, String okText, DimengSingleButtonAlertDialog.OnDialogClickListener onDialogClickListener) {
        return showAlertDialog("", message, okText, onDialogClickListener);
    }

    protected DimengSingleButtonAlertDialog showAlertDialog(String title, String message, String okText, DimengSingleButtonAlertDialog.OnDialogClickListener onDialogClickListener) {
        return showAlertDialog(title, message, okText, false, onDialogClickListener);
    }

    protected DimengSingleButtonAlertDialog showAlertDialog(String title, String message, String okText, boolean cancelable, DimengSingleButtonAlertDialog.OnDialogClickListener onDialogClickListener) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("AlertDialogFragment");
        if (fragment != null) {
            // 为了不重复显示dialog，在显示对话框之前移除正在显示的对话框
            fragmentTransaction.remove(fragment);
        }
        DimengSingleButtonAlertDialog dialogFragment = DimengSingleButtonAlertDialog.newInstance(title, message);
        dialogFragment.setAgreeText(okText);
        dialogFragment.setOnDialogClickListener(onDialogClickListener);
        dialogFragment.setCanceledOnTouchOutside(cancelable);
        dialogFragment.show(fragmentTransaction, "SingleButtonAlertDialog");// 显示一个Fragment并且给该Fragment添加一个Tag，可通过findFragmentByTag找到该Fragment
        return dialogFragment;
    }

    protected DimengConfirmAlertDialog showMessageConfirmDialog(String message, String okText, String cancelText, DimengConfirmAlertDialog.OnDialogClickListener onDialogClickListener) {
        return showMessageConfirmDialog("", message, okText, cancelText, onDialogClickListener);
    }

    protected DimengConfirmAlertDialog showMessageConfirmDialog(String title, String message, String okText, String cancelText, DimengConfirmAlertDialog.OnDialogClickListener onDialogClickListener) {
        return showMessageConfirmDialog(title, message, okText, cancelText, false, onDialogClickListener);
    }

    protected DimengConfirmAlertDialog showMessageConfirmDialog(String title, String message, String okText, String cancelText, boolean cancelable, DimengConfirmAlertDialog.OnDialogClickListener onDialogClickListener) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("MessageConfirmDialogFragment");
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        }
        DimengConfirmAlertDialog dialogFragment = DimengConfirmAlertDialog.newInstance(title, message);
        dialogFragment.setAgreeText(okText);
        dialogFragment.setNotAgreeText(cancelText);
        dialogFragment.setOnDialogClickListener(onDialogClickListener);
        dialogFragment.setCanceledOnTouchOutside(cancelable);
        dialogFragment.show(fragmentTransaction, "ConfirmAlertDialog");
        return dialogFragment;
    }

    protected DimengTwoPwdEditAlertDialog showTwoPwdEditAlertDialog(String title, String okText, String cancelText, DimengTwoPwdEditAlertDialog.OnDialogClickListener onDialogClickListener) {
        return showTwoPwdEditAlertDialog(title, okText, cancelText, false, onDialogClickListener);
    }

    protected DimengTwoPwdEditAlertDialog showTwoPwdEditAlertDialog(String title, String okText, String cancelText, boolean cancelable, DimengTwoPwdEditAlertDialog.OnDialogClickListener onDialogClickListener) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("TwoPwdEditAlertDialogFragment");
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        }
        DimengTwoPwdEditAlertDialog dialogFragment = DimengTwoPwdEditAlertDialog.newInstance(title);
        dialogFragment.setAgreeText(okText);
        dialogFragment.setNotAgreeText(cancelText);
        dialogFragment.setOnDialogClickListener(onDialogClickListener);
        dialogFragment.setCanceledOnTouchOutside(cancelable);
        dialogFragment.show(fragmentTransaction, "TwoPwdEditAlertDialogFragment");
        return dialogFragment;
    }

    protected DimengOneEditAlertDialog showOneEditAlertDialog(String title, String okText, String cancelText, DimengOneEditAlertDialog.OnDialogClickListener onDialogClickListener) {
        return showOneEditAlertDialog(title, okText, cancelText, false, onDialogClickListener);
    }

    protected DimengOneEditAlertDialog showOneEditAlertDialog(String title, String okText, String cancelText, boolean cancelable, DimengOneEditAlertDialog.OnDialogClickListener onDialogClickListener) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("OneEditAlertDialogFragment");
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        }
        DimengOneEditAlertDialog dialogFragment = DimengOneEditAlertDialog.newInstance(title);
        dialogFragment.setAgreeText(okText);
        dialogFragment.setNotAgreeText(cancelText);
        dialogFragment.setOnDialogClickListener(onDialogClickListener);
        dialogFragment.setCanceledOnTouchOutside(cancelable);
        dialogFragment.show(fragmentTransaction, "OneEditAlertDialog");
        return dialogFragment;
    }


}